package main

import (
	"fmt"
	"net/rpc/jsonrpc"
	"time"
)

type AddRequest struct {
	Left  int
	Right int
}

type AddResponse struct {
	Result int
}

type Calc struct {
}

func (c *Calc) Add(req AddRequest, resp *AddResponse) error {
	fmt.Println("calc.add")
	resp.Result = req.Left + req.Right
	return nil
}

func main() {
	client, _ := jsonrpc.Dial("tcp", "127.0.0.1:8888")
	req := AddRequest{3, 66}
	resp := AddResponse{}

	//同步调用
	//err := client.Call("Calc.Add", req, &resp)
	//fmt.Println(err, resp)

	//异步调用
	call := client.Go("Calc.Add", req, &resp, nil)
	for {
		select {
		case result := <-call.Done:
			fmt.Println(result.Reply, result.Error)
			return
		default:
			fmt.Println(time.Now())
			time.Sleep(1 * time.Second)
		}
	}
}
